<?php

namespace App\Remote\EKuaiBao;

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Utils;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpFoundation\Request as RequestAlias;

class EKuaiBaoRemote extends BaseRemote
{
    /**
     * 获取token
     *
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function getAccessToken()
    {
        return self::customRequestV2(
            "/api/openapi/v1/auth/getAccessToken",
            RequestAlias::METHOD_POST,
            [
                "appKey"      => "13381f73-d6f5-42f9-95ca-fb7066103b2e",
                "appSecurity" => "4d16fb20-bc9b-492f-802b-6101ad819cf4"
            ],
            null,
            null,
            60 * 60 * 2 - 10
        );
    }

    /**
     * 员工信息
     *
     * @param array $conditionIds
     * @param string $type
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function staff(array $conditionIds, string $type = "CELLPHONE")
    {
        $requestConditionIds = [];
        collect($conditionIds)->each(function($v)use(&$requestConditionIds){
            if(str_contains($v, "+86")){
                array_push($requestConditionIds, $v);
                array_push($requestConditionIds, str_replace("+86", "", $v));
            }else{
                array_push($requestConditionIds, $v);
                array_push($requestConditionIds, "+86".$v);
            }
        });
        return self::customRequestV2(
            "/api/openapi/v1/staffs/getStaffIds?accessToken=" . self::getAccessToken()["value"]["accessToken"],
            RequestAlias::METHOD_POST,
            [
                "type"         => $type,
                "conditionIds" => $requestConditionIds
            ]
        );
    }


    /**
     * 创建单据
     *
     * @param array $formContent
     * @param string $isCommit
     * @param string $isUpdate
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function flowData(array $formContent, string $isCommit = "true", string $isUpdate = "false")
    {
        return self::customRequestV2(
            "/api/openapi/v2.2/flow/data?" . http_build_query([
                "accessToken" => self::getAccessToken()["value"]["accessToken"],
                "isCommit"    => $isCommit,
                "isUpdate"    => $isUpdate
            ]),
            RequestAlias::METHOD_POST,
            $formContent
        );
    }


    /**
     * 附件上传
     *
     * @param string $name
     * @param string $file
     * @return mixed|void
     * @throws GuzzleException|ValidationException
     */
    public static function attachmentUpload(string $name, string $file)
    {
        return self::customRequestV2(
            "/api/openapi/v2/attachment/upload?accessToken=" . self::getAccessToken()["value"]["accessToken"],
            RequestAlias::METHOD_POST,
            [
                'multipart' => [
                    [
                        'name'     => 'name',
                        'contents' => $name
                    ],
                    [
                        'name'     => 'file',
                        'contents' => Utils::tryFopen($file, 'r'),
                        'filename' => $file,
                    ]
                ]
            ],
            ["Content-Type" => "multipart/form-data"]
        );
    }

    /**
     * 创建收款账户
     *
     * @param string $username
     * @param string $bankName
     * @param string $cardNo
     * @param string $staffId
     * @param string $branch
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function createPayeeInfos(
        string $username,
        string $bankName,
        string $cardNo,
        string $staffId,
        string $branch
    ) {
        return self::customRequestV2(
            "/api/openapi/v2.1/payeeInfos?accessToken=" . self::getAccessToken()["value"]["accessToken"],
            RequestAlias::METHOD_POST,
            [
                "type"       => "PERSONAL",
                "owner"      => "INDIVIDUAL",
                "name"       => $username,
                "cardNo"     => $cardNo,
                "bank"       => $bankName,
                "staffId"    => $staffId,
                "branch"     => $branch,
                "visibility" => [
                    "fullVisible" => true
                ]
            ]
        );
    }

    /**
     * @param string $cardNo
     * @param bool $throwException
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function payeeInfos(string $cardNo, bool $throwException = false)
    {
        return self::customRequestV2(
            "/api/openapi/v2/payeeInfos?" . http_build_query([
                "accessToken" => self::getAccessToken()["value"]["accessToken"],
                "cardNos"     => $cardNo,
                "start"       => 0,
                "count"       => 100
            ]),
            RequestAlias::METHOD_GET,
            null,
            null,
            null,
            0,
            $throwException
        );
    }

    /**
     * 更改收款信息
     *
     * @param string $payeeInfoId
     * @param string $bankCardNo
     * @param string $bankName
     * @param string $branchBankName
     * @param string $staffId
     * @param bool $throwException
     * @return mixed|void
     * @throws GuzzleException|ValidationException
     */
    public static function updatePayeeInfos(
        string $payeeInfoId,
        string $bankCardNo,
        string $bankName,
        string $branchBankName,
        string $staffId,
        bool $throwException = false
    ) {
        return self::customRequestV2(
            "/api/openapi/v2.1/payeeInfos/\${$payeeInfoId}?" . http_build_query([
                "accessToken" => self::getAccessToken()["value"]["accessToken"]
            ]),
            RequestAlias::METHOD_PUT,
            [
                "owner"      => "INDIVIDUAL",
                "type"       => "PERSONAL",
                "name"       => $bankName,
                "visibility" => [
                    "fullVisible" => true
                ],
                "staffId"    => $staffId,
                "cardNo"     => $bankCardNo,
                "branch"     => $branchBankName
            ],
            null,
            null,
            0,
            $throwException
        );
    }


    /**
     * 获取审批状态
     *
     * @param string $flowId
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function approveStates(string $flowId)
    {
        return self::customRequestV2(
            "/api/openapi/v1.1/approveStates/[$flowId]?accessToken=" . self::getAccessToken()["value"]["accessToken"],
            RequestAlias::METHOD_GET
        );
    }

    /**
     * 单据详情获取
     *
     * @param string $flowId
     * @param bool $throwException
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function flowDetails(string $flowId, bool $throwException = false)
    {
        return self::customRequestV2(
            "/api/openapi/v1.1/flowDetails?".http_build_query([
                "accessToken" => self::getAccessToken()["value"]["accessToken"],
                "flowId"      => $flowId
            ]),
            RequestAlias::METHOD_GET,
            null,
            null,
            null,
            0,
            $throwException
        );
    }

    /**
     * @param string $id
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function getSpecificationsByIds(string $id)
    {
        return self::customRequestV2(
            "/api/openapi/v2/specifications/byIds/[$id]?" . http_build_query([
                "accessToken" => self::getAccessToken()["value"]["accessToken"]
            ]),
            RequestAlias::METHOD_GET
        );
    }

    /**
     * 根据ID或CODE获取费用类型模板信息
     *
     * @param string $id
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function getFeeTypeSpecificationsByIds(string $id)
    {
        return self::customRequestV2(
            "/api/openapi/v2/specifications/feeType/byIdsAndCodes?accessToken=" . self::getAccessToken()["value"]["accessToken"],
            RequestAlias::METHOD_POST,
            [
                "ids" => [$id]
            ]
        );
    }

    /**
     * 根据名称获取部门信息
     *
     * @param string $name
     * @return mixed|void
     * @throws GuzzleException
     */
    public static function getDepartmentByName(string $name)
    {
        return self::customRequestV2(
            "/api/openapi/v1/departments/getDepartmentByName?" . http_build_query([
                "accessToken" => self::getAccessToken()["value"]["accessToken"],
                "name"        => $name
            ]),
            RequestAlias::METHOD_GET
        );
    }

    /**
     * 获取支行信息
     *
     * @return mixed
     * @throws GuzzleException
     * @throws ValidationException
     */
    public static function getAllBranch()
    {
        return self::customRequestV2(
            "/api/openapi/v1/banks/getAllBranch?" . http_build_query([
                "accessToken" => self::getAccessToken()["value"]["accessToken"],
            ]),
            RequestAlias::METHOD_POST
        );
    }
}
